package com.ktwlsoft.archivemanage.domain.business.repositoryService;

import com.alibaba.fastjson.JSONObject;
import com.ktwlsoft.archivemanage.domain.dao.entity.BasisManage;
import com.ktwlsoft.archivemanage.domain.dao.repository.BasisManageMapper;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;


/**
 * @author JokerMo
 * @version 1.0
 * @date 2020/4/28 14:15
 */

@Service
public class BasisManageRepositoryService {
    public final BasisManageMapper basisManageMapper;

    public BasisManageRepositoryService(BasisManageMapper basisManageMapper) {
        this.basisManageMapper = basisManageMapper;
    }

    /**
     * 分页查询
     *
     * @param pn
     * @param pageSize
     * @param basisName
     * @return
     */
    public PageInfo<BasisManage> findPage(int pn, int pageSize, String basisName) {
        PageHelper.startPage(pn, pageSize);
        PageInfo<BasisManage> pageInfo = new PageInfo<>(basisManageMapper.findPage(basisName));
        return pageInfo;
    }

    /**
     * 获取所有
     *
     * @return JSONObject
     */
    public List<JSONObject> getAll() throws UnsupportedEncodingException {
        List<BasisManage> basisManageList = basisManageMapper.selectAll();
        List<JSONObject> resList = new ArrayList<>();
        for (BasisManage basisManage : basisManageList) {
            JSONObject jsonObject = new JSONObject();
            String content = new String(basisManage.getContent(), "utf-8");
            jsonObject.put("id", basisManage.getId());
            jsonObject.put("basisName", basisManage.getBasisName());
            jsonObject.put("createName", basisManage.getCreateName());
            jsonObject.put("createId", basisManage.getCreateId());
            jsonObject.put("createTime", basisManage.getCreateTime());
            jsonObject.put("content", content);
            resList.add(jsonObject);
        }
        return resList;
    }

    /**
     * id获取记录
     *
     * @param id
     * @return
     */
    public BasisManage getById(Long id) {
        return basisManageMapper.selectByPrimaryKey(id);
    }

    /**
     * 依据新增
     *
     * @param basisManage
     * @return
     */
    public int insert(BasisManage basisManage) {
        return basisManageMapper.insert(basisManage);
    }

    /**
     * 依据更新
     *
     * @param basisManage
     * @return
     */
    public int update(BasisManage basisManage) {
        return basisManageMapper.updateByPrimaryKey(basisManage);
    }

    /**
     * id删除记录
     *
     * @param id
     * @return
     */
    public int deleteById(Long id) {
        return basisManageMapper.deleteByPrimaryKey(id);
    }

    /**
     * 重复校验
     *
     * @param basisName
     * @return
     */
    public BasisManage selectByBasisName(String basisName) {
        return basisManageMapper.selectByBasisName(basisName);
    }
}
